package cn.zhibang.sannuo.pharmacy.service.impl;

import cn.zhibang.sannuo.account.service.AAccountnumderService;
import cn.zhibang.sannuo.commons.model.Accountnumber;
import cn.zhibang.sannuo.commons.model.BranchOffice;
import cn.zhibang.sannuo.commons.model.HeadOffice;
import cn.zhibang.sannuo.commons.utils.SaltUtil;
import cn.zhibang.sannuo.pharmacy.dao.HeadOfficeDao;
import cn.zhibang.sannuo.pharmacy.dao.PBranchOfficeDao;
import cn.zhibang.sannuo.pharmacy.service.HeadOfficeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.*;
import java.util.List;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName HeadOfficeServiceImpl.java
 * @Description TODO
 * @createTime 2021年04月06日 14:12:00
 */
@Service
@Transactional
public class HeadOfficeServiceImpl implements HeadOfficeService {

    @Autowired private HeadOfficeDao headOfficeDao;
    @Autowired private PBranchOfficeDao pBranchOfficeDao;
    @Autowired private AAccountnumderService aAccountnumderService;

    @Override
    public List<HeadOffice> selAllHeadOffice(String hname, String hnumder) {
        return headOfficeDao.selectAllHeadOffice(hname, hnumder);
    }

    @Override
    public void upHeadOffice(HeadOffice headOffice) {
        headOfficeDao.updateHeadOffice(headOffice);
    }

    @Override
    public String selMaxHnumber() {
        return headOfficeDao.selectMaxHnumber();
    }

    @Override
    public Accountnumber addHeadOffice(HeadOffice headOffice) {
        headOfficeDao.insertHeadOffice(headOffice);
        Integer hid = headOffice.getHid();
        BranchOffice branchOffice = new BranchOffice();
        branchOffice.setHeadOffice(headOffice);
        branchOffice.setBname(headOffice.getHname());
        branchOffice.setBnumber(headOffice.getHnumder());
        branchOffice.setBcontacts(headOffice.getHcontacts());
        branchOffice.setBphone(headOffice.getHphone());
        branchOffice.setBtelephone(headOffice.getHtelephone());
        branchOffice.setBaddress(headOffice.getHaddress());
        pBranchOfficeDao.insertBranchOffice(branchOffice);

        String salt = SaltUtil.createSalt();
        String password = SaltUtil.salt("Zong" + headOffice.getHnumder(), salt);

        Accountnumber accountnumber = new Accountnumber();
        accountnumber.setAccount("Zong"+headOffice.getHnumder());
        accountnumber.setAname(headOffice.getHcontacts());
        accountnumber.setApssword(password);
        accountnumber.setBranchOffice(branchOffice);
        accountnumber.setSalt(salt);
        aAccountnumderService.addAccountnumber(accountnumber);
        aAccountnumderService.addAccountRole(accountnumber.getAid(), 3);
        return accountnumber;
    }
}
